#!bin/bash

PATTERN_PATH=/mnt/e/project-pattern/
DEPLOY_PATH=$PATTERN_PATH/.environment/deploy

#获取centos镜像
  if [[ -z "$(docker images -q centos:latest 2> /dev/null)" ]]; then
      docker pull centos:latest
  else
      echo "centos installed"
  fi

# 添加jdk  jdk链接可能失效在https://jdk.java.net/java-se-ri/15重新获取
  mkdir -p /opt/jdk
  if [[ -e "/opt/jdk/openjdk-15+36_linux-x64_bin.tar.gz" ]]; then
      echo "jdk is haved"
  else
      wget -P /opt/jdk/ https://download.java.net/openjdk/jdk15/ri/openjdk-15+36_linux-x64_bin.tar.gz
      cd /opt/jdk
      tar -xvzf /opt/jdk/openjdk-15+36_linux-x64_bin.tar.gz
  fi

#构建镜像
  if [[ -z "$(docker images -q pattern-boot:01 2> /dev/null)" ]]; then
      cd /opt/jdk
      docker  build  -t  pattern-boot:01 . -f  $DEPLOY_PATH/Dockerfile
  else
      echo "pattern-boot build success"
  fi
cd $DEPLOY_PATH

#运行容器
if [[ -z "$(docker ps -aqf "name=pattern-boot")" ]]; then
    mkdir -p /opt/project-pattern/
    docker  run -d -p 19527:9527 -p 18081:18080 --name pattern-boot \
    -v /opt/project-pattern:/var/app/server/ \
    --add-host=project-pattern:172.31.11.185 \
    --restart=always --privileged=true -it pattern-boot:01 /bin/bash
    docker exec -it pattern-boot bash -c "yum -y install unzip"
else
    echo "pattern-boot running"
fi

#安装maven  连接失效重新获取http://maven.apache.org/download.cgi
  mkdir -p /opt/maven
  if [[ -e "/opt/maven/apache-maven-3.8.4-bin.tar.gz" ]]; then
      echo "maven is haved"
  else
      wget -P /opt/maven/ https://apache.osuosl.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz
      cd /opt/maven/
      tar -xvzf /opt/maven/apache-maven-3.8.4-bin.tar.gz
      mv /opt/maven/apache-maven-3.8.4 /opt/maven/apache-maven-3.8.1
  fi

 \cp -r $DEPLOY_PATH/settings.xml /opt/maven/apache-maven-3.8.1/conf

export PATH=/opt/jdk/jdk-15/bin:$PATH
echo $PATH

#打包后端项目
cd $PATTERN_PATH
git pull
#
rm -rf /opt/project-pattern/*
/opt/maven/apache-maven-3.8.1/bin/mvn clean
/opt/maven/apache-maven-3.8.1/bin/mvn \
--update-snapshots -DskipTests=true package -P uat

#打包前端项目
cd web
yarn
yarn build:prod

mkdir -p /project-pattern/web/
\cp -r $PATTERN_PATH/web/dist/* /opt/web/dist/

cd $DEPLOY_PATH

#复制项目至容器
\cp -r $PATTERN_PATH/run.sh /opt/project-pattern/
mv -f /project-pattern/package/* /opt/project-pattern/
chmod 777 /opt/project-pattern/*

#容器内启动项目
docker exec -i pattern-boot bash -c "/var/app/server/run.sh unzip_restart springbootadmin"
docker exec -i pattern-boot bash -c "/var/app/server/run.sh unzip_restart socketserver"
docker exec -i pattern-boot bash -c "/var/app/server/run.sh unzip_restart pattern-gateway"
docker exec -i pattern-boot bash -c "/var/app/server/run.sh unzip_restart fileboot"
docker exec -i pattern-boot bash -c "/var/app/server/run.sh unzip_restart mainboot"
docker exec -i pattern-boot bash -c "/var/app/server/run.sh unzip_restart smsboot"

#查看日志
#docker exec -i pattern-boot bash -c "/var/app/server/run.sh script mainboot log"
#重启
#docker exec -i pattern-boot bash -c "/var/app/server/run.sh script mainboot restart"
#状态
#docker exec -i pattern-boot bash -c "/var/app/server/run.sh script mainboot status"

#字体问题
#复制windows字体到docker /usr/share/fonts/
#docker cp /usr/share/fonts/ mall-boot:/usr/share/fonts/
#进入容器
#fc-cache -fv
#source /etc/profile
#查看中文字体
#fc-list :lang=zh